package com.yx.cloud.activity;

import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioGroup;

import com.iot.product.sweeper.bean.CleanSettings;
import com.iot.sweeper.AppContext;
import com.iot.sweeper.param.CleanSettingParam;
import com.yx.cloud.R;
import com.yx.cloud.base.BaseActivity;
import com.yx.cloud.databinding.CleaningmopsettingsActivityBinding;
import com.yx.cloud.utils.Intent_utils;
import com.yx.cloud.utils.RomUtils;
import com.iot.product.sweeper.bean.Robot;
import com.iot.common.utils.Event;
import com.iot.common.utils.LogUtils;

import org.greenrobot.eventbus.EventBus;import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class CleaningMopSettingsActivity extends BaseActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
    private CleaningmopsettingsActivityBinding binding;

    @Override
    protected void onCreate() {
        super.onCreate();
        binding = CleaningmopsettingsActivityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        if (!EventBus.getDefault().isRegistered(this)) {//加上判断
            EventBus.getDefault().register(this);
        }
        initData();
    }

    private void initData() {
        Robot device = AppContext.get().getCurrentDevice();
        if (device != null) {
            CleanSettings settings = device.getCleanSettings();
            boolean cleanMopSwitch = settings.isCleanMop();
            binding.cbCleaningMopSwitch.setChecked(cleanMopSwitch);//清洗拖布开关
            automaticCleaningMopSwitch(cleanMopSwitch);

            boolean drySwitch = settings.isDrySwitch();//烘干开关
            binding.cbAutomaticDryingSwitch.setChecked(drySwitch);
            dryingSwitch(drySwitch);

            int frequency = settings.getBackwashFrequency(); //回洗频率
            setBackWaskFrequency(frequency);
            int cleanMopFrequency = settings.getCleanMopFrequency();//清洗拖布次数
            setCleanMopTimes(cleanMopFrequency);

            String dryingTime = settings.getDryingTime();
            dryingTime(dryingTime);//烘干时间
        }
        binding.ivCleanMopSettingReturn.setOnClickListener(this);
        binding.btnWashImmediately.setOnClickListener(this);
        binding.rgCleanMopBackwashFrequency.setOnCheckedChangeListener(this);
        binding.rgCleanTimes.setOnCheckedChangeListener(this);
        binding.rgDryingTime.setOnCheckedChangeListener(this);
        binding.cbCleaningMopSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.isPressed()) {
                    //自动清洗拖布开关
                    LogUtils.i("自动清洗拖布开关 : " + isChecked);
                    AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().cleanMop(isChecked).build());
                }
            }
        });
        binding.cbAutomaticDryingSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (compoundButton.isPressed()) {
                    //自动烘干开关
                    LogUtils.i("自动烘干开关 : " + isChecked);
                    AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().drySwitch(isChecked).build());
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        if (RomUtils.isFastClick()) {
            return;
        }
        int id = view.getId();
        if (id == R.id.iv_cleanMopSettingReturn) {
            Intent_utils.backIntent(this);
        } else if (id == R.id.btn_washImmediately) {
            AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().cleanMop(true).build());
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(Event event) {
        switch (event.getType()) {
            case Event.MSG_ROBOT_SETTINGS:
                //回洗频率
                int frequency = (int) event.getContent();
                setBackWaskFrequency(frequency);
                break;
            case Event.MESSAGE_TYPE_DRYING_TIME:
                //烘干时间
                String dryingTime = (String) event.getContent();
                dryingTime(dryingTime);
                break;
            case Event.MESSAGE_TYPE_CLEAN_MOP_FREQUENCY:
                //清洗拖布次数
                int cleanMopFrequency = (int) event.getContent();
                setCleanMopTimes(cleanMopFrequency);
                break;
            case Event.MESSAGE_TYPE_DRY_SWITCH:
                //自动烘干开关
                boolean drySwitch = (boolean) event.getContent();
                binding.cbAutomaticDryingSwitch.setChecked(drySwitch);
                dryingSwitch(drySwitch);
                break;
            case Event.MESSAGE_TYPE_CLEAN_MOP:
                //清洗拖布开关
                boolean cleanMop = (boolean) event.getContent();
                //LogUtils.i("清洗拖布开关 : " + cleanMop);
                binding.cbCleaningMopSwitch.setChecked(cleanMop);
                automaticCleaningMopSwitch(cleanMop);
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        int id = radioGroup.getId();
        if (id == R.id.rg_cleanMop_BackwashFrequency) {
            //回洗频率
            setBackwashFrequency(radioGroup, checkedId);
        } else if (id == R.id.rg_cleanTimes) {
            //清洗次数
            setCleanTimes(radioGroup, checkedId);
        } else if (id == R.id.rg_dryingTime) {
            //烘干时间
            setDryingTime(radioGroup, checkedId);
        }
    }

    /**
     * 设置回洗频率
     */
    private void setBackwashFrequency(RadioGroup group, int checkedId) {
        View viewById = group.findViewById(checkedId);
        if (!viewById.isPressed()) {
            return;
        }
        int frequency = 0;
        if (checkedId == R.id.rb_BackwashFrequency10) {
            frequency = 10;
        } else if (checkedId == R.id.rb_BackwashFrequency20) {
            frequency = 20;
        } else if (checkedId == R.id.rb_BackwashFrequency30) {
            frequency = 30;
        }
        AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().backwashFrequency(frequency).build());
    }

    /**
     * 设置初始化设置回洗频率
     */
    public void setBackWaskFrequency(int frequency) {
        if (frequency == 10) {
            binding.rbBackwashFrequency10.setChecked(true);
        } else if (frequency == 20) {
            binding.rbBackwashFrequency20.setChecked(true);
        } else if (frequency == 30) {
            binding.rbBackwashFrequency30.setChecked(true);
        }
    }

    /**
     * 清洗拖布次数
     *
     * @param group
     * @param checkedId
     */
    private void setCleanTimes(RadioGroup group, int checkedId) {
        View viewById = group.findViewById(checkedId);
        if (!viewById.isPressed()) {
            return;
        }
        int cleanTimes = 0;
        if (checkedId == R.id.rg_cleanTimesOne) {
            cleanTimes = 1;
        } else if (checkedId == R.id.rg_cleanTimesTwo) {
            cleanTimes = 2;
        } else if (checkedId == R.id.rg_cleanTimesThree) {
            cleanTimes = 3;
        }
        AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().setCleanMopFrequency(cleanTimes).build());
    }

    /**
     * 设置初始化清洗拖布次数
     */
    public void setCleanMopTimes(int times) {
        if (times == 1) {
            binding.rgCleanTimesOne.setChecked(true);
        } else if (times == 2) {
            binding.rgCleanTimesTwo.setChecked(true);
        } else if (times == 3) {
            binding.rgCleanTimesThree.setChecked(true);
        }
    }

    private void setDryingTime(RadioGroup group, int checkedId) {
        View viewById = group.findViewById(checkedId);
        if (!viewById.isPressed()) {
            return;
        }
        String dryingTime = "";
        if (checkedId == R.id.rg_dryingTimeTwo) {
            dryingTime = "low";
        } else if (checkedId == R.id.rg_dryingTimeThree) {
            dryingTime = "middle";
        } else if (checkedId == R.id.rg_dryingTimeFour) {
            dryingTime = "high";
        }
        AppContext.getProductService().setDeviceCleanSettings(CleanSettingParam.newBuilder().setDryingTime(dryingTime).build());
    }

    /**
     * 设置初始化清洗拖布次数
     */
    public void dryingTime(String times) {
        if (times.equals("low")) {
            binding.rgDryingTimeTwo.setChecked(true);
        } else if (times.equals("middle")) {
            binding.rgDryingTimeThree.setChecked(true);
        } else if (times.equals("high")) {
            binding.rgDryingTimeFour.setChecked(true);
        }
    }

    private void automaticCleaningMopSwitch(boolean cleanMopSwitch) {
        if (cleanMopSwitch) {
            binding.llBackwashFrequency.setVisibility(View.VISIBLE);
        } else {
            binding.llBackwashFrequency.setVisibility(View.GONE);
        }
    }

    private void dryingSwitch(boolean drySwitch) {
        if (drySwitch) {
            binding.llDryingTime.setVisibility(View.VISIBLE);
        } else {
            binding.llDryingTime.setVisibility(View.GONE);
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
